package com.github_dididipapa.mall.product.service.impl;


import com.github_dididipapa.mall.product.dao.AttrAttrgroupRelationDao;
import com.github_dididipapa.mall.product.entity.AttrAttrgroupRelationEntity;
import com.github_dididipapa.mall.product.entity.AttrEntity;
import com.github_dididipapa.mall.product.service.AttrAttrgroupRelationService;
import com.github_dididipapa.mall.product.service.AttrService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github_dididipapa.common.utils.PageUtils;
import com.github_dididipapa.common.utils.Query;

import com.github_dididipapa.mall.product.dao.AttrGroupDao;
import com.github_dididipapa.mall.product.entity.AttrGroupEntity;
import com.github_dididipapa.mall.product.service.AttrGroupService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@RequiredArgsConstructor
@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    @Lazy
    private AttrService attrService;
    private final AttrGroupDao attrGroupDao;
    private final AttrAttrgroupRelationDao attrAttrgroupRelationDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();

        //key有值时 才进行模糊查询
        if(StringUtils.hasText(key)){
            wrapper.and(
                    (innerSql) ->{
                        innerSql.eq("attr_group_id",key)
                                .or()
                                .like("attr_group_name",key);
                    });
        }

        //catelogId == 0 查询所有分页
        if(catelogId == 0){
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
             return new PageUtils(page);
        }

        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                wrapper.eq("catelog_id",catelogId)
        );
        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupEntity> getAttrGroupWithAttrs(Long catelogId) {
        //得到attr_group_id
        List<AttrGroupEntity> list = attrGroupDao.getAttrGroup(catelogId);
        // 遍历list中的每个item，为每个item设置与其关联的属性列表
        list.stream().forEach(item -> {
            // 通过item的类别ID获取与其关联的属性列表
            List<AttrEntity> relationAttr = attrGroupDao.getRelationAttr(item.getCatelogId());
            // 将获取到的属性列表设置给item
            item.setAttrs(relationAttr);
        });

        return list;
    }

    /**
     * 根据属性组ID获取属性列表
     * 
     * @param attrGroupId 属性组ID
     * @return 包含属性实体对象的列表，如果未找到相关属性则返回null
     */
    @Transactional(readOnly = true)
    @Override
    public List<AttrEntity> getAttrByAttrGroupId(Long attrGroupId) {
        // 查询属性组关联的所有属性关系
        List<AttrAttrgroupRelationEntity> entities = attrAttrgroupRelationDao.selectList(
                new QueryWrapper<AttrAttrgroupRelationEntity>()
                        .eq("attr_group_id", attrGroupId));
        // 提取所有属性ID
        List<Long> attrIds = entities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
        if(attrIds.isEmpty()){
            return null;
        }
        // 根据属性ID列表查询具体属性信息
        return attrService.listByIds(attrIds);
    }



}